package patrol.mobile.com.mobilepatrol.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.DialogUtil;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.utils.StringUtils;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.view.IBaseView;

import static patrol.mobile.com.mobilepatrol.activity.CommonListActivity.ROWS;

/**
 * Created by apple on 17/9/26.
 */

public abstract class BaseActivity extends AppCompatActivity implements IBaseView {
    @Nullable
    @BindView(R.id.iv_back)
    public ImageView iv_back;
    @Nullable
    @BindView(R.id.tv_center_title)
    public TextView tv_center_title;
    //    @Nullable
//    @BindView(R.id.iv_back2)
//    public ImageView iv_back2;
    @Nullable
    @BindView(R.id.iv_right_title)
    public ImageView iv_right_title;
    @Nullable
    @BindView(R.id.tv_right_title)
    public TextView tv_right_title;
    public String baseUrl;
    public String current_id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(attachLayoutRes());
        baseUrl = (String) SPUtils.get(Constants.current_ser, "");
        current_id = (String) SPUtils.get(Constants.current_id, "");
        ButterKnife.bind(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        initView();
        initData();

        setListener();

    }

    public void initData() {

    }

    public void initView() {
        initTitle();
    }

    public void initTitle() {

    }
//
//    @Override
//    protected void onStart() {
//        super.onStart();
//        boolean screen = (boolean) SPUtils.get(Constants.screen,true);
//        if (screen){
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }else{
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        }
//    }

    /**
     * 绑定布局文件
     *
     * @return 布局文件ID
     */
    @LayoutRes
    protected abstract int attachLayoutRes();

    public boolean checkPermission(@NonNull String permission) {
        return ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * [设置监听]
     */
    public abstract void setListener();

    /**
     * [简化Toast]
     *
     * @param msg
     */
    public void showToast(String msg) {
        ToastUtils.showToast(msg);
    }

    public void showToast2(String msg) {
        ToastUtils.showToast2(msg);
    }

    /**
     * [页面跳转]
     */
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(BaseActivity.this, clz));
    }

    /**
     * dialog
     */
    private Dialog loadingDialog = null;

    public void showLoadingPanel(boolean cancelable) {
        if (loadingDialog != null) {
            if (!loadingDialog.isShowing()) {
                loadingDialog.show();
            }
            return;
        }
        loadingDialog = DialogUtil.showLoadingDialog(this, cancelable, null);
    }

    public boolean isDialogShowing() {
        return loadingDialog != null && loadingDialog.isShowing();
    }

    public void hideLoadingPanel() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            try {
                loadingDialog.dismiss();
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    @Override
    public void showLoading() {
        showLoadingPanel(false);
    }

    @Override
    public void hideLoading() {
        hideLoadingPanel();
    }

    @Override
    public void onBackPressed() {
        CommonUtil.exitActivityAndBackAnim(this, true);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // land do nothing is ok
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            // port do nothing is ok
        }
    }

    protected <T> ArrayAdapter<T> getArrayAdapter(T[] param) {
        ArrayAdapter<T> adapter = new ArrayAdapter<T>(this, android.R.layout.simple_spinner_dropdown_item, param);
        adapter.setDropDownViewResource(R.layout.spinner_item_view);
        return adapter;
    }

    protected <T> ArrayAdapter<T> getArrayAdapter(List<T> param) {
        ArrayAdapter<T> adapter = new ArrayAdapter<T>(this, android.R.layout.simple_spinner_dropdown_item, param);
        adapter.setDropDownViewResource(R.layout.spinner_item_view);
        return adapter;
    }

    protected <T> void selectedItem(Spinner spi, T selectItem) {
        if (spi == null || selectItem == null || selectItem.equals(""))
            return;
        SpinnerAdapter adapter = spi.getAdapter();
        for (int i = 0; i < adapter.getCount(); i++) {
            if (adapter.getItem(i).toString().equals(selectItem)) {
                spi.setSelection(i);
                break;
            }
        }
    }

    protected int getIntegerValue(EditText editText) {
        int result = -1;

        String str = editText.getText().toString();
        if (!StringUtils.isEmpty(str))
            result = Integer.valueOf(str);

        return result;
    }

    protected void hiddenInputKey() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (getCurrentFocus() != null)
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

    }

    public void setData(BaseQuickAdapter mAdapter, int page, boolean isRefresh, List data, int rows) {
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.updateItems(data);
        } else {
            int totalPage;
            if ((int) Math.ceil(rows / ROWS) == 0) {
                totalPage = 1;
            } else {
                totalPage = (int) Math.ceil(rows / ROWS);
            }
            if (page == totalPage) {
                if (page == 1) {
                    mAdapter.updateItems(data);
                    mAdapter.enableLoadMore(false);//加载loading gone掉
                } else {
                    mAdapter.addItems(data);
                    mAdapter.noMoreData();
                }
            } else {
                mAdapter.addItems(data);
//                mAdapter.enableLoadMore(false);//加载loading gone掉
                mAdapter.loadComplete();//本次加载完成
            }
        }
    }
}
